package com.example.administrator.meishi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.meishi.R;
import com.example.administrator.meishi.activity.NewActivity;
import com.example.administrator.meishi.bean.MediaTitle;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/9/21.
 */
public class PicAdapter extends BaseAdapter {

    private List<MediaTitle> mlist;
    private final LayoutInflater layoutInflater;
    private Context mcontext;

    public PicAdapter(List<MediaTitle> list, Context context) {
        this.mlist = list;
        this.mcontext=context;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {

        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item, parent, false);
            viewHolder=new ViewHolder();
            viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
            viewHolder.text = (TextView) convertView.findViewById(R.id.tx);
            viewHolder.linear = (LinearLayout) convertView.findViewById(R.id.linearlayout);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final MediaTitle b = mlist.get(position);
        String recommend_caption = b.getRecommend_caption();
        String recommend_cover_pic = b.getRecommend_cover_pic();
        viewHolder.text.setText(recommend_caption);
        Picasso.with(mcontext).load(recommend_cover_pic).placeholder(R.mipmap.ic_launcher).config(Bitmap.Config.RGB_565).into(viewHolder.image);
        //点击每个条目，进入详情界面
        viewHolder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(parent.getContext(), NewActivity.class);
                intent.putExtra("video",b.getVideo());
                intent.putExtra("text",b.getCaption());
                parent.getContext().startActivity(intent);
            }
        });
        return convertView;
    }
    static class ViewHolder{
            ImageView image;
            TextView text;
        LinearLayout linear;
    }
}
